﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFTray
{
	public partial class WndVisoinSetting : Form
	{
		public WndVisoinSetting()
		{
			InitializeComponent();
			TopLevel = false;

			BackColor = Color.MediumAquamarine;
			flowLayoutPanel3.BackColor = SystemColors.Control;
			binaryParams1.BackColor = SystemColors.Control;
			roiPanel1.BackColor = SystemColors.Control;
			snapExposureLights1.BackColor = SystemColors.Control;
			matchParamPanel1.BackColor = SystemColors.Control;
			boxSelectMark1.BackColor = SystemColors.Control;

			comboBoxMethod.DisplayMember = "Item1";
			comboBoxMethod.ValueMember = "Item2";
			List<Tuple<string, int>> items = new List<Tuple<string, int>>()
			{
				new Tuple<string, int>("圆形定位",0),
				new Tuple<string, int>("矩形定位",1),
				new Tuple<string, int>("模板匹配",2),
			};
			comboBoxMethod.DataSource = items;
			comboBoxMethod.SelectedIndexChanged += (ss, ee) =>
			{
				int sv = (int)comboBoxMethod.SelectedValue;
				matchParamPanel1.Enabled = (sv == 2);
				boxSelectMark1.FocuseEnabled = (sv == 2);
				textBoxTol.Enabled = (sv != 2);
			};
			comboBoxMethod.SelectedValue = 2;

			VisibleChanged += (s, e) =>
			  {
				  if (Visible == false)
				  {
					  ParentHide();
					  return;
				  }

				  if (Feed == null) return;
				  Data2UI();
			  };
		}

		internal void ParentHide()
		{
		}

		private FeedTray Feed=null;
		internal int ID = 0;
		internal void Init(FeedTray feed,int id)
		{
			ID = id;
			Feed = feed;
			Data2UI();
		}

		private void Data2UI()
		{
			var cvp = Feed.GetVision(ID);

			checkBoxEnabled.Checked = cvp.enabled;
			comboBoxMethod.SelectedValue = cvp.method;
			textBoxTol.Text = cvp.tolerancePer.ToString();

			binaryParams1.BinMethod = cvp.binm;
			binaryParams1.ParamValue = cvp.binp;

			snapExposureLights1.EnableSnap = cvp.snap.Enabled;
			snapExposureLights1.Exposure = cvp.snap.Exposure;
			snapExposureLights1.LightBack = cvp.snap.BrightnessBack;
			snapExposureLights1.LightFore = cvp.snap.BrightnessFore;
			roiPanel1.X = cvp.roi_x;
			roiPanel1.Y = cvp.roi_y;
			roiPanel1.W = cvp.roi_w;
			roiPanel1.H = cvp.roi_h;

			boxSelectMark1.InitialDir = Path.GetDirectoryName(GlobalParams.CD.GetRecipeFile());
			boxSelectMark1.DefaultFileName = Feed.Name + ID.ToString();
			//boxSelectMark1.DefaultFileName= Path.GetFileNameWithoutExtension(GlobalParams.CD.GetRecipeFile());
			boxSelectMark1.SetMarkFilename(cvp.mark_fn);
			boxSelectMark1.PointClickedImage = new PointF(cvp.focusX, cvp.focusY);

			//match
			matchParamPanel1.Similarity = cvp.matchParam.similarity;
			matchParamPanel1.RequireCount = cvp.matchParam.requireCount;
			matchParamPanel1.NumFeatures = cvp.matchParam.num_features;
			matchParamPanel1.WeakThresh = cvp.matchParam.weak_threshold;
			matchParamPanel1.StrongThresh = cvp.matchParam.strong_threshold;
			matchParamPanel1.DegreeBegin = cvp.matchParam.degree_begin;
			matchParamPanel1.DegreeEnd = cvp.matchParam.degree_end;
			matchParamPanel1.DegreeStep = cvp.matchParam.step_degree;
			matchParamPanel1.Pyramid = cvp.matchParam.pyramid;
			matchParamPanel1.MaxCount = cvp.matchParam.MaxCount;
		}
		internal bool UI2Data()
		{
			try
			{
				var cvp = Feed.GetVision(ID);
				cvp.tolerancePer = (int)Convert.ToUInt16(textBoxTol.Text);
				cvp.enabled = checkBoxEnabled.Checked;
				cvp.method = (int)comboBoxMethod.SelectedValue;
				cvp.binm = binaryParams1.BinMethod;
				cvp.binp = binaryParams1.ParamValue;

				cvp.snap.Enabled = snapExposureLights1.EnableSnap;
				cvp.snap.Exposure = snapExposureLights1.Exposure;
				cvp.snap.BrightnessBack = (ushort)snapExposureLights1.LightBack;
				cvp.snap.BrightnessFore = (ushort)snapExposureLights1.LightFore;

				cvp.roi_x = roiPanel1.X;
				cvp.roi_y = roiPanel1.Y;
				cvp.roi_w = roiPanel1.W;
				cvp.roi_h = roiPanel1.H;

				string mark_fn = boxSelectMark1.GetMarkFile();
				if (string.IsNullOrEmpty(mark_fn))
				{
					cvp.mark_fn = "";
				}
				else if (mark_fn == "NOSAVE")
				{
					MessageBox.Show(this, "框选MARK未保存，还使用原来MARK", "提醒");
				}
				else
				{
					cvp.mark_fn = mark_fn;
				}
				cvp.focusX = boxSelectMark1.PointClickedImage.X;
				cvp.focusY = boxSelectMark1.PointClickedImage.Y;

				//match
				cvp.matchParam.similarity = matchParamPanel1.Similarity;
				cvp.matchParam.requireCount = matchParamPanel1.RequireCount;
				cvp.matchParam.num_features = matchParamPanel1.NumFeatures;
				cvp.matchParam.weak_threshold = matchParamPanel1.WeakThresh;
				cvp.matchParam.strong_threshold = matchParamPanel1.StrongThresh;
				cvp.matchParam.degree_begin = matchParamPanel1.DegreeBegin;
				cvp.matchParam.degree_end = matchParamPanel1.DegreeEnd;
				cvp.matchParam.step_degree = matchParamPanel1.DegreeStep;
				cvp.matchParam.pyramid = matchParamPanel1.Pyramid;
				cvp.matchParam.MaxCount = matchParamPanel1.MaxCount;
			}
			catch (Exception ex)
			{
				System.Diagnostics.Debug.WriteLine(ex.Message);
				return false;
			}
			return true;
		}
	}
}
